// pages/cash-details/cash-details.js
const apiurl = require('../../config').apiurl;
import {
  Pulibc
} from '../../models/public.js'
const pulibc = new Pulibc();
Page({
  // 下拉

  // 关闭记录
  ChengClone() {
    this.setData({
      showTap: false
    })
  },
  // 显示记录
  ShowShade() {
    // 初始化提现数据
    if(this.data.list.length==0){
      this.getShade(1)
    }
    this.setData({
      showTap: true
    })
  },
  // 获取记录
  getShade(page) {
    let url = apiurl + '/cashmanagement/cashmalist'
    let key = wx.getStorageSync('key');
    console.log(key)
    wx.request({
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      url: url,
      method: 'POST',
      data: {
        key,
        page,
        withdrawal_text: []
      },
      success: data => {
        console.log(data)
        if (data.data.code == 200) {
          // 每页加载10条数据
          if (data.data.datas.list.length == 10) {
            this.setData({
              showMore: true
            })
          } else {
            this.setData({
              showMore: false
            })
          }
          var list = this.data.list.concat(data.data.datas.list)
          this.setData({
            list
          })
        }
      }
    })
  },

  // 支付宝提现
  getAlipay() {
    let phoneReg = /^1[34578]\d{9}$/;
    let url = apiurl + '/cashmanagement/addcash';
    let tradc_amount = this.data.tradc_amount
    let tradc_bank_no = this.data.tradc_bank_no
    let phone = this.data.phone
    let tradc_bank_user = this.data.tradc_bank_user
    let key = wx.getStorageSync('key')
    let type_id = this.data.type_val

    if (tradc_amount < 10) {
      pulibc.showToast('输入金额不能低于10元', 'none')
      return;
    }
    if (tradc_amount > this.data.info.money) {
      pulibc.showToast('输入金额有错', 'none')
      return;
    }
    if (tradc_amount.trim() == '') {
      pulibc.showToast('请输入金额', 'none')
      return;
    }
    if (tradc_bank_no.trim() == '') {
      pulibc.showToast('请输入账号', 'none')
      return;
    }
    if (phone.trim() == '' || !phoneReg.test(phone)) {
      pulibc.showToast('请输入正确手机号码', 'none')
      return;
    }
    if (tradc_bank_user.trim() == '') {
      pulibc.showToast('请输入姓名', 'none')
      return;
    }
    console.log(key)
    wx.request({
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      url: url,
      method: 'POST',
      data: {
        type: type_id,
        key,
        tradc_amount,
        tradc_bank_no,
        phone,
        tradc_bank_user

      },
      success: data => {
        console.log(data)
        let res = data.data.datas
        let data_code = data.data.code
        if (data_code == 200) {
          pulibc.showToast('提现成功', 'none')

          this.setData({
            tradc_amount: '',
            tradc_bank_no: '',
            phone: '',
            tradc_bank_user: ''
          })
        } else if (data_code == 400) {
          pulibc.showToast(res.error, 'none')

        } else {
          pulibc.showToast('系统异常稍后再试', 'none')

        }
      }
    })
  },

  // 微信提现
  getWx() {

    let url = apiurl + '/cashmanagement/addcash';
    let tradc_amount = this.data.tradc_amount
    let key = wx.getStorageSync('key')
    let type_id = this.data.type_val

    if (tradc_amount < 10) {
      pulibc.showToast('输入金额不能低于10元', 'none')
      return;
    }
    if (tradc_amount > this.data.info.money) {
      pulibc.showToast('输入金额有错', 'none')
      return;
    }
    if (tradc_amount.trim() == '') {
      pulibc.showToast('请输入金额', 'none')
      return;
    }

    console.log(key)
    wx.request({
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      url: url,
      method: 'POST',
      data: {
        type: type_id,
        key,
        tradc_amount,
      },
      success: data => {
        console.log(data)
        let res = data.data.datas
        let data_code = data.data.code
        if (data_code == 200) {
          pulibc.showToast('提现成功', 'none')

          this.setData({
            tradc_amount: '',
            tradc_bank_no: '',
            phone: '',
            tradc_bank_user: ''
          })
        } else if (data_code == 400) {
          pulibc.showToast(res.error, 'none')

        } else {
          pulibc.showToast('系统异常稍后再试', 'none')

        }
      }
    })
  },

  // 提交提现
  getInputVal() {
    // 1是支付宝提现 2是微信提现
    let type_val = this.data.type_val
    if (type_val == 1) {
      console.log('支付宝提现')
      this.getAlipay()
    } else if (type_val == 2) {
      console.log('微信提现')
      this.getWx();
    }
  },
  // 获取信息
  getDataUser() {
    let url = apiurl + '/memberaccount/info'
    let key = wx.getStorageSync('key')
    wx.request({
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      url: url,
      method: 'POST',
      data: {
        key
      },
      success: data => {
        console.log(data)
        let data_code = data.data.code
        if (data_code == 200) {
          
          let info = data.data.datas.member_info
          let withdrawal_text = data.data.datas.withdrawal_text
          let totalMoney = parseFloat(info.money + info.generation_money).toFixed(2);
          this.setData({
            info,
            withdrawal_text,
            totalMoney
          })
        }else if(data_code == 400){
          pulibc.navigateUrl('../register/register');
        }
         else {
          pulibc.showToast(data.data.datas.error, 'none')
        }
      }

    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    showTap: false,
    page: 1,
    list: [],
    tradc_amount: '', // 提现金额
    tradc_bank_no: '', // 收款账号
    phone: '', // 手机号码
    tradc_bank_user: '', // 姓名
    info: {},
    listType: [{
      type: 1,
      type_name: '支付宝'
    }, {
      type: 2,
      type_name: '微信'
    }],
    type_name: '',
    type_id: '0',
    type_val: 1,
    showMore: false, //加载更多数据
    totalMoney: 0

  },
  // 类型切换
  TapTypeChange(e) {

    let index = e.detail.value;

    this.data.listType.forEach((item, idx) => {
      if (idx == index) {
        this.setData({
          type_id: index,
          type_name: item.type_name,
          type_val: item.type
        })
      }
    })

  },

  // 用户账号
  getTradcBankNo(e) {

    this.setData({
      tradc_bank_no: e.detail.value
    })
  },
  // 用户名
  getTradcBankUser(e) {
    this.setData({
      tradc_bank_user: e.detail.value
    })
  },
  // 手机号码
  getPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 提现金额
  getTradcAmount(e) {
    this.setData({
      tradc_amount: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (wx.getStorageSync("key") == false) {
      wx.navigateTo({
        url: "../register/register"
      })
    } else {
      this.getDataUser()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (this.data.showTap) {
      console.log('456')
    }
    console.log(1231);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.showTap) {
      console.log(123)
    }



  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  showLoadmore() {
    let page = this.data.page;
    let showMore = this.data.showMore;
    if (this.data.list.length == parseInt(page + '0')) {
      page++;
      this.getShade(page);
    }
    if (!showMore) {
      pulibc.showToast('没有更多数据了', 'none')
    }




  }
})